package univ.uchiha.esiag.gon.subscriber.impl;

import java.io.Serializable;
import java.util.Vector;

import univ.uchiha.esiag.gon.accesser.DataReactor;
import univ.uchiha.esiag.gon.operation.Operation;
import univ.uchiha.esiag.gon.subscriber.Subscriber;

public class SubscriberImpl implements Subscriber{
	private Vector<DataReactor> dataReactors;
	public SubscriberImpl() {
		dataReactors = new Vector<DataReactor>();
	}
	public DataReactor newDataReactor(){
		DataReactor dataReactor = new DataReactor();
		dataReactors.add(dataReactor);
		return dataReactor;
	}
	public boolean hasTopic(String name){
		for(int i = 0;i<dataReactors.size();i++){
			if(name.equals(dataReactors.get(i).getTopicName())){
				return true;
			}
		}
		return false;
	}
	public void publish(Serializable obj) {
		final Operation ope = (Operation)obj;
		new Thread(new Runnable() {
			public void run() {
				for(int i = 0;i<dataReactors.size();i++){
					if(dataReactors.get(i).getTopicName().equals(ope.getTopicNameTarget())){
						dataReactors.get(i).put(ope);
					}
				}
			}
		}).start();
	}
}
